package com.oha.laporrayelchurro.service;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;

import com.oha.laporrayelchurro.commons.Util;

public class H2DataBaseService {
	
	private static BasicDataSource h2DS=null;

	/**
	 * 
	 * @return
	 * @throws SQLException
	 */
	public Connection getConnection() throws SQLException{
		if(h2DS==null){
			createDS();
		}
    	return h2DS.getConnection();
	}
	
	/**
	 * 
	 */
	private static void createDS(){
		h2DS = new BasicDataSource();
		h2DS.setUsername("sa");
		h2DS.setPassword("");
		h2DS.setDriverClassName("org.h2.Driver");
		String url = Util.getUsrDir().concat("/data/db/porra/porra");
		h2DS.setUrl("jdbc:h2:file:".concat(url));
		h2DS.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		h2DS.setMaxIdle(0);//Numero maximo de conexiones inactivas
	}
}
